#pragma once
#include "UICommon.h"
#include "CommonWnd.h"
#include "RenderContext.h"
#include "StratumRender.h"

#define MAINWND_CLASS_NAME			_T("MainWnd")

class CMainWnd : public CCommonWnd
			   , public IPreMessageFilterUI
{
public:
	CMainWnd();
	LPCTSTR GetWindowClassName() const { return MAINWND_CLASS_NAME; };

protected:
	BEGIN_DUINOTIFY_MAPEX(CCommonWnd)
		ON_DUINOTIFY_TYPE("windowinit", OnWindowInit)
		ON_DUINOTIFY_TYPE("windowdestroy", OnWindowDestroy)
		ON_DUINOTIFY_TYPE("idle", OnIdle)
		ON_DUINOTIFY_CONTROL("click", "open_button", OnOpenButtonClicked)
		ON_DUINOTIFY_CONTROL("click", "bkcolor_button", OnBkColorButtonClicked)
		ON_DUINOTIFY_MESSAGE("menuitemselect", "menuitem_open", OnOpenButtonClicked)
		ON_DUINOTIFY_MESSAGE("menuitemselect", "menuitem_exit", OnCloseButtonClicked)
		ON_DUINOTIFY_MESSAGE("menuitemselect", "menuitem_volume_method", OnMenuItemVolumeMethod)
		ON_DUINOTIFY_MESSAGE("menuitemselect", "menuitem_3dsimulate_method", OnMenuItem3DSimulateMethod)
		ON_DUINOTIFY_CONTROL("itemselect", "meshsize_combo", OnMeshSizeChanged)
		ON_DUINOTIFY_CONTROL("itemselect", "interp_combo", OnInterpChanged)
		ON_DUINOTIFY_CONTROL("selectchanged", "drill_button", OnDrillClicked)
		ON_DUINOTIFY_CONTROL("selectchanged", "stratum_button", OnStratumClicked)
	END_DUINOTIFY_MAPEX()
	
	void OnWindowInit(TNotifyUI& msg);
	void OnWindowDestroy(TNotifyUI& msg);
	void OnIdle(TNotifyUI& msg);
	void OnOpenButtonClicked(TNotifyUI& msg);
	void OnBkColorButtonClicked(TNotifyUI& msg);

	void OnMenuItemVolumeMethod(TNotifyUI& msg);
	void OnMenuItem3DSimulateMethod(TNotifyUI& msg);
	void OnMeshSizeChanged(TNotifyUI& msg);
	void OnInterpChanged(TNotifyUI& msg);
	void OnDrillClicked(TNotifyUI& msg);
	void OnStratumClicked(TNotifyUI& msg);

	bool OnMenuClicked(void* param);
	bool OnVisibleClicked(void* param);

	void UpdateLeftList();

	virtual void Init();
	LRESULT PreMessageHandler(const LPMSG pMsg, bool& bHandled);

	void PopupMenu(CButtonUI* pMenuButton, LPCTSTR szMenuXmlFile);

private:
	CRealWndUI* m_pRenderWnd;
	COptionUI* m_pDrillOption;
	COptionUI* m_pStratumOption;
	COptionUI* m_pMeshOption;
	COptionUI* m_pLayerOption;
	COptionUI* m_pSolidOption;
	COptionUI* m_pMoveOption;
	COptionUI* m_pScaleOption;
	COptionUI* m_pRotateOption;

	struct SMenuButton
	{
		CButtonUI* pButton;
		CStdString strButtonName;
		CStdString strMenuXmlFile;
	};
	typedef std::vector<SMenuButton> CMenuButtonVector;
	typedef std::vector<SMenuButton>::iterator CMenuButtonIterator;
	CMenuButtonVector m_cMenuButtons;

	CRenderContext m_cRenderContext;
	float m_fZScale;
	CArray2D<glm::vec3>* m_pDrillData;
	glm::vec4* m_pPorosityData;
	UINT m_uiPorosityCount;
	glm::vec4* m_pSaturationData;
	UINT m_uiSaturationCount;
	BOOL m_bDataLoaded;
	COLORREF m_dwBackgroundColor;
	CStratumRender* m_pStratumRender;
	BOOL m_bShadeByDensity;

	std::vector<texstring> m_cDrillNames;
	std::vector<texstring> m_cStratumNames;

	std::vector<bool> m_cDrillVisibilities;
	std::vector<bool> m_cStratumVisibilities;

	static DWORD WINAPI BuildModelProc(LPVOID lpParam);
	static void BuildModelCallback(int nPercent, LPVOID pContext);
};
